package com.rent.apartment.controller;

import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rent.common.annotation.Log;
import com.rent.common.enums.BusinessType;
import com.rent.apartment.domain.ApartmentInfo;
import com.rent.apartment.service.IApartmentInfoService;
import com.rent.common.core.controller.BaseController;
import com.rent.common.core.domain.AjaxResult;
import com.rent.common.utils.poi.ExcelUtil;
import com.rent.common.core.page.TableDataInfo;

/**
 * 公寓信息Controller
 *
 * @author cooper
 * @date 2024-10-09
 */
@Controller
@RequestMapping("/apartment/apartmentInfo")
public class ApartmentInfoController extends BaseController
{
    /** 定义前缀路径 */
    private String prefix = "apartment/apartmentInfo";

    /** 注入公寓信息服务 */
    @Autowired
    private IApartmentInfoService apartmentInfoService;

    /**
     * 访问公寓信息页面
     */
    @RequiresPermissions("apartment:apartmentInfo:view")
    @GetMapping()
    public String apartmentInfo()
    {
        return prefix + "/apartmentInfo";
    }

    /**
     * 查询公寓信息列表
     *
     * @param apartmentInfo 查询条件
     * @return 公寓信息列表数据
     */
    @RequiresPermissions("apartment:apartmentInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApartmentInfo apartmentInfo)
    {
        startPage();
        List<ApartmentInfo> list = apartmentInfoService.selectApartmentInfoList(apartmentInfo);
        return getDataTable(list);
    }

    /**
     * 导出公寓信息列表
     *
     * @param apartmentInfo 查询条件
     * @return 导出结果
     */
    @RequiresPermissions("apartment:apartmentInfo:export")
    @Log(title = "公寓信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApartmentInfo apartmentInfo)
    {
        List<ApartmentInfo> list = apartmentInfoService.selectApartmentInfoList(apartmentInfo);
        ExcelUtil<ApartmentInfo> util = new ExcelUtil<ApartmentInfo>(ApartmentInfo.class);
        return util.exportExcel(list, "公寓信息数据");
    }

    /**
     * 跳转到新增公寓信息页面
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公寓信息
     *
     * @param apartmentInfo 公寓信息
     * @return 保存结果
     */
    @RequiresPermissions("apartment:apartmentInfo:add")
    @Log(title = "公寓信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApartmentInfo apartmentInfo)
    {
        return toAjax(apartmentInfoService.insertApartmentInfo(apartmentInfo));
    }

    /**
     * 跳转到修改公寓信息页面
     *
     * @param id 公寓ID
     * @param mmap 模型对象
     * @return 修改页面
     */
    @RequiresPermissions("apartment:apartmentInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApartmentInfo apartmentInfo = apartmentInfoService.selectApartmentInfoById(id);
        mmap.put("apartmentInfo", apartmentInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存公寓信息
     *
     * @param apartmentInfo 公寓信息
     * @return 保存结果
     */
    @RequiresPermissions("apartment:apartmentInfo:edit")
    @Log(title = "公寓信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApartmentInfo apartmentInfo)
    {
        return toAjax(apartmentInfoService.updateApartmentInfo(apartmentInfo));
    }

    /**
     * 删除公寓信息
     *
     * @param ids 需要删除的数据ID字符串
     * @return 删除结果
     */
    @RequiresPermissions("apartment:apartmentInfo:remove")
    @Log(title = "公寓信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] idStrings = ids.split(",");
        Long[] longIds = Arrays.stream(idStrings)
                .map(idStr -> Long.parseLong(idStr.trim()))
                .toArray(Long[]::new);
        return toAjax(apartmentInfoService.deleteApartmentInfoByIds(longIds));
    }

    /**
     * 获取指定城市的社区列表
     *
     * @param city 城市名称
     * @return 社区列表
     */
    @GetMapping("/neighborhoods")
    @ResponseBody
    public AjaxResult getNeighborhoodsByCity(String city) {
        List<String> neighborhoods = apartmentInfoService.selectApartmentInfoByCity(city);
        return AjaxResult.success(neighborhoods);
    }

    /**
     * 根据城市和社区获取小区列表
     * @param city 城市名称
     * @param neighborhood 社区名称
     * @return 小区列表
     */
    @GetMapping("/communities")
    @ResponseBody
    public AjaxResult getCommunitiesByCityAndNeighborhood(String city, String neighborhood) {
        List<String> communities = apartmentInfoService.selectCommunitiesByCityAndNeighborhood(city, neighborhood);
        return AjaxResult.success(communities);
    }
}